package com.roy.comet;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * Created by mc on 18/4/13.
 */
@Component
public class CometRouter {

    private static final String DB_TABLE_SEP = "_";

    private List<CometTable> cometTables;

    private HashMap<String, CometTable> cometTablesMapFull;

    private HashMap<String, CometTable> cometTablesMap;

    public CometRouter() {

    }

    public void init() {
        cometTablesMapFull = new HashMap<String, CometTable>();
        cometTablesMap = new HashMap<String, CometTable>();
        for (int i = 0; i < cometTables.size(); i++) {
            CometTable cometTable = cometTables.get(i);
            String key = constructKey(cometTable.getDbName(),cometTable.getTableName());
            cometTablesMapFull.put(key, cometTable);
            cometTablesMap.put(cometTable.getTableName(), cometTable);
        }
    }

    public CometTable searchCometTable( String tableName) {
        return cometTablesMap.get(tableName);
    }
    private String constructKey(String dbName, String tableName) {
        return dbName + DB_TABLE_SEP + tableName;
    }


    public CometRouter(List<CometTable> cometTables) {
        this.cometTables = cometTables;
    }

    public static String getDbTableSep() {
        return DB_TABLE_SEP;
    }

    public List<CometTable> getCometTables() {
        return cometTables;
    }

    public void setCometTables(List<CometTable> cometTables) {
        this.cometTables = cometTables;
    }

    public HashMap<String, CometTable> getCometTablesMapFull() {
        return cometTablesMapFull;
    }

    public void setCometTablesMapFull(HashMap<String, CometTable> cometTablesMapFull) {
        this.cometTablesMapFull = cometTablesMapFull;
    }

    public HashMap<String, CometTable> getCometTablesMap() {
        return cometTablesMap;
    }

    public void setCometTablesMap(HashMap<String, CometTable> cometTablesMap) {
        this.cometTablesMap = cometTablesMap;
    }
}
